/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package menus;

import com.sun.lwuit.Form;
import com.sun.lwuit.List;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import util.Menu;

/**
 *
 * @author nialldeasy
 */
public class Landmarks extends Menu {

    Vector landmarks = null;
    String location = "";

    protected void execute(Form f) {

        this.setBackround("ice.jpg");
        routine();
        this.setLayout(4);
        this.addList(getList(), location);
    }

    public void recycleBin() {
        landmarks = null;
    }

    public void update() {
    }

    public double[] get_position(String landmark) {
        if (landmarks==null) {
            routine();
        }

        double[] pos = new double[2];
        for (int i = 0; i < landmarks.size(); i++) {
            Vector stringdoublearray = (Vector) landmarks.elementAt(i);
            String name = (String) stringdoublearray.elementAt(0);
            pos = (double[]) stringdoublearray.elementAt(1);
            if (name.equalsIgnoreCase(landmark)) {
                return pos;
            }
        }
        return null;
    }

    public String[] getList() {
        String[] list = new String[landmarks.size()];
        for (int i = 0; i < landmarks.size(); i++) {
            list[i] = ((String) ((Vector) landmarks.elementAt(i)).elementAt(0));
        }
        return list;
    }

    public String readFile() {
     //   OutputStream os = new OutputStream() {};
        InputStream is = getClass().getResourceAsStream("/Landmarks/landmarks.txt");
        StringBuffer sb = new StringBuffer();
        try {
            int chars, i = 0;
            while ((chars = is.read()) != -1) {

                sb.append((char) chars);
            }
            return sb.toString();
        } catch (Exception e) {
        }
        return null;
    }

    public void routine() {
        landmarks = new Vector();
        location = "";
        String read = readFile();
        String name = "";
        int s = -1;
        double[] pos = new double[2];
        String lon = "";
        String lat = "";
        for (int i = 0; i < read.toCharArray().length; i++) {
            switch (read.charAt(i)) {
                case '{':
                    s = -1;
                    break;
                case '}':
                    s = 0;
                    break;
                case '$':
                    i = read.toCharArray().length;
                    return;
                case '<':
                    s = 0;
                    name = "";
                    lat = "";
                    lon = "";
                    break;
                case '>':
                    s = 1;
                    break;
                case '[':
                    s = 1;
                    break;
                case ']':
                    pos[0] = Double.parseDouble(lon);
                    pos[1] = Double.parseDouble(lat);
                    add(name, pos);
                    break;
                case ',':
                    s = 2;
                    break;
                default:
                    switch (s) {
                        case -1:
                            location += read.charAt(i);
                            break;
                        case 0:
                            name += read.charAt(i);
                            break;
                        case 1:
                            lon += read.charAt(i);
                            break;
                        case 2:
                            lat += read.charAt(i);
                            break;
                    }
                    break;
            }
        }
    }

    public void add(String name, double[] pos) {
        Vector stringdoublearray = new Vector();
        stringdoublearray.addElement(name);
        stringdoublearray.addElement(pos);
        landmarks.addElement(stringdoublearray);
    }
}
